home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / mail / sendmail-conf.frm.z / sendmail-conf.frm
Encoding:
Text File  |  1997-07-30  |  11.2 KB  |  331 lines

  1. #! /usr/bin/perl5
  2. #
  3. # sendmail-conf.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: sendmail-conf.frm,v 1.25 1997/06/19 22:25:50 shotes Exp $
  21.  
  22. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  23. require "/usr/OnRamp/lib/OnRamp.pm";
  24. require "/usr/OnRamp/lib/java.pm";
  25. require "flush.pl";
  26.  
  27. $query = new CGI;
  28.  
  29. $cmd = "/usr/etc/configmail";
  30. $prm = "/etc/sendmail.params";
  31. $tmpprm = "/etc/sendmail.params.tmp";
  32. $MAILSCRIPT = "/etc/init.d/mail";
  33. $title = "Sendmail Configuration";
  34.  
  35. if ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla\/2/) { $br_index = 1; }
  36. else { $br_index = 0; }
  37.  
  38. $js = 
  39. "$js_check_int
  40. which = \"none\";
  41. br_index = $br_index;
  42. function runSubmit() {
  43.     // onClick gets processed before onSubmit
  44.     if(which == \"ok\") return runOK();
  45.     return (true);
  46. }
  47. function markOK() { which = \"ok\"; }
  48. function markOther() { which = \"none\"; }
  49. function runOK() {
  50.     form = document.SendmailForm;
  51.     if (form.enable[br_index].checked) { // radio buttons are backwards
  52.         if (!testHostname(form.forwarder,\"forwarding host name\")) return (false);
  53.         if (!testHostname(form.rootdomain,\"root domain name\")) return (false);
  54.         if (!testHostname(form.relayname,\"relay host name\")) return (false);
  55.         if (!testHostname(form.localdomain,\"local domain name\")) return (false);
  56.         if (!testDirectory(form.pathalias)) return (false);
  57.         if (!testHostList(form.directdomains)) return (false);
  58.     } return (true);
  59. }
  60. function testDirectory(Ctrl) {
  61.     word = Ctrl.value;
  62.     if (word == \"\") { errorBox (Ctrl, \"The UUCP path-alias directory is required.\\nTo disable the path-alias database, enter \\n/dev/null as the directory.\"); Ctrl.value = \"/dev/null\"; return (false); } 
  63.     for(i = 0; i < word.length; i++) {
  64.         if (word.charAt(i) == ' ') { errorBox (Ctrl, \"The UUCP directory cannot \\ncontain spaces.\"); return (false); }
  65.     }
  66.     if (word.charAt(0) != '/') { errorBox (Ctrl, \"The UUCP directory must be \\nfully qualified.\"); return (false); }
  67.     return (true);
  68. }
  69. function testHostname(Ctrl,what) {
  70.     if (Ctrl.value == \"\") { errorBox (Ctrl, \"The \" + what + \" is required.\"); return (false); } 
  71.     error = testHost(Ctrl.value);
  72.     if (error == 1) { errorBox (Ctrl, \"The \" + what + \" cannot \\ncontain \" + illegal + \" characters.\"); return (false); }
  73.     if (error == 2) { errorBox (Ctrl, \"The \" + what + \" cannot start or \\n end with a . or - or _ character.\"); return (false); }
  74.     if (error == 3) { errorBox (Ctrl, \"The \" + what + \" cannot contain \\nconsecutive . or - or _ characters.\"); return (false); }
  75.     if (error == 4) { errorBox (Ctrl, \"The \" + what + \" cannot contain spaces.\"); return (false); }
  76.     if (error == 5) { errorBox (Ctrl, \"The \" + what + \" cannot begin \\nwith a number.\"); return (false); }
  77.     return (true);
  78. }
  79. function testHostList(Ctrl) {
  80.     whitespace = \" \\n\\r\\t\\f\";  space = true;
  81.     for(start=0, cur=0; cur < Ctrl.value.length; cur++) {
  82.         for(i = 0; i < whitespace.length; i++) {
  83.             c = whitespace.charAt(i);
  84.             if (Ctrl.value.charAt(cur) == c) { // found whitespace
  85.                 if (space == false) { space = true;  if(cur != start) { host = Ctrl.value.substring(start,cur); if (testHost(host)) { errorBox (Ctrl, \"Invalid relay host name: \" + host); return (false); } } } break;
  86.             } else {                           // found character 
  87.                 if (space == true) { space = false;  start = cur; }
  88.             }
  89.         }
  90.     }
  91.     if(cur != start && space == false) { host = Ctrl.value.substring(start,cur); if (testHost(host)) { errorBox (Ctrl, \"Invalid relay host name: \" + host); return (false); } }
  92.     return (true);
  93. }
  94. function testHost(word) {
  95.     metaChars = \"{}()*!~<>?|[]'&^\$\@\#`\\\";:\\\\/+=,%\";
  96.     start = 0;  while (word.charAt(start) == ' ') { start++; }
  97.     end = word.length-1; while (word.charAt(end) == ' ') { end--; }
  98.     for(j = 0; j < metaChars.length; j++) {
  99.         c = metaChars.charAt(j); i = word.indexOf(c, 0); if (i != -1) { illegal = c; return 1; }
  100.     }
  101.     sch = word.charAt(start); ech = word.charAt(end);
  102.     if(sch == '.' || sch == '-' || sch == '_') return 2;
  103.     if(ech == '.' || ech == '-' || ech == '_') return 2;
  104.     for(j = start; j <= end; j++) { 
  105.     ch = word.charAt(j); 
  106.     if (ch == '.' || ch == '-' || ch == '_') { 
  107.         if (word.charAt(j+1) == ch) return 3; 
  108.     } 
  109.     if (ch == ' ') { return 4; } }
  110.     if(checkInt_c(sch, 10)) return 5;
  111.     return 0;
  112. }
  113. function errorBox (Ctrl, ErrorMessage) {
  114.     alert (ErrorMessage);  Ctrl.focus();  return;
  115. }";
  116.  
  117. @supported = (    'forwarder',
  118.         'rootdomain',
  119.         'relayname',
  120.         'localdomain',
  121.         'pathalias',
  122.         'directdomains' );
  123.  
  124. print $query->header;
  125.  
  126. &getValues;
  127.  
  128. if ($query->param) {
  129.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  130.     $help =~ s/cgi$/hlp/;
  131.     exec $help if ($query->param('help') eq "Help");
  132.  
  133.     if ($query->param('doit') eq 'Ok') {
  134.     if ($query->param('enable') eq 'Yes') { &formValidation; }
  135.  
  136.     if ($hash{'enable'} ne $query->param('enable')) {
  137.         if ($query->param('enable') eq 'Yes') {
  138.     system("/etc/chkconfig", "sendmail", "on");
  139.         $message = "Sendmail enabled.";
  140.         } else { 
  141.     system("/etc/chkconfig", "sendmail", "off");
  142.         $message = "Sendmail disabled."; 
  143.         }
  144.         &flush(stdout);
  145.         if (!fork) {
  146.     system($MAILSCRIPT, "stop");
  147.     system($MAILSCRIPT, "start");
  148.         exit 0;
  149.         }
  150.     }
  151.  
  152.     if ($query->param('enable') eq 'Yes') {
  153.         undef %fileList;
  154.         open(PRM, "< $prm");
  155.         while(<PRM>) {
  156.         chomp();
  157.         ($left,$right) = split(/:/);
  158.             if ($left && $right) {
  159.         $fileList{$left} = $right;
  160.             }
  161.         }
  162.         close(PRM);
  163.  
  164.         $mod = 0;
  165.  
  166.         foreach $arg (@supported) { $vals{$arg} = $query->param($arg) }
  167.  
  168.         foreach $arg (keys(%vals)) {
  169.         if ($vals{$arg} ne $hash{$arg}) {
  170.             $mod = 1;
  171.             $fileList{$arg} = $vals{$arg};
  172.         }
  173.         }
  174.  
  175.         if ($mod == 1) {
  176.         open(PRMT, ">$tmpprm");
  177.         foreach $arg (keys(%fileList)) {
  178.             print PRMT "$arg:$fileList{$arg}\n";
  179.         }
  180.         close(PRMT);
  181.         rename($tmpprm, $prm);
  182.     system($MAILSCRIPT, "start");
  183.  
  184.         $message .= "  Configuration file updated.";
  185.         }
  186.     }
  187.     }
  188.     if (!$message) { $message = "No changes made."; }
  189. }
  190.  
  191. $NSstatus = &checkForNS;
  192.  
  193. if ($NSstatus) { 
  194.     &title_block($title);
  195.     &header_block($title);
  196.     print "<i>NS Mail is currently running.  To enable sendmail, first
  197.         disable NS Mail.</i>";
  198. } else { &generic; }
  199.  
  200. exit 0;
  201.  
  202. sub checkForNS {
  203.     local($mail_process) = "NscpMail";
  204.     local($ret) = 0;
  205.     open(PSNS, "/bin/ps -ef |");
  206.     while(<PSNS>) {
  207.     if ($_ =~ m:$mail_process:) { $ret = 1; }
  208.     }
  209.     close(PSNS);
  210.     $ret;
  211. }
  212.  
  213. sub error {
  214.     &error_block($_[0]);
  215.     &generic;
  216.     exit 0;
  217. }
  218.  
  219. sub getValues {
  220.     $hash{'enable'} = &get_config("sendmail");
  221.    
  222.     open(LIST, "$cmd list |");
  223.     @configlist = <LIST>;
  224.     close(LIST);
  225.  
  226.     for ($i = 0; $i < $#configlist; $i++) {
  227.     $configlist[$i] =~ s/[\"\s+]//g;
  228.     ($attr, $aval) = split(/=/, $configlist[$i]);
  229.     if ($attr ne "") {
  230.         $hash{$attr} = $aval; chop($aval);
  231.     }
  232.     }
  233.     $hash{'directdomains'} =~ s/,/\n/g;
  234. }
  235.  
  236. sub formValidation {
  237.     my $error;
  238.  
  239.     $error = &check_hostname($query->param('forwarder'));
  240.     &error("Forwarding host name cannot contain shell metacharacters") if $error == 911;
  241.     &error("Invalid forwarding host name") if $error;
  242.  
  243.     $localDomain = $query->param('localdomain');
  244.     &error("Invalid local domain name.") 
  245.         if (!$localDomain || &check_hostname($localDomain));     
  246.  
  247.     $error = &check_hostname($query->param('rootdomain'));
  248.     &error("Root domain name cannot contain shell metacharacters") if $error == 911;
  249.     &error("Invalid root domain name") if $error;
  250.  
  251.     my $directs = $query->param('directdomains');
  252.     $directs =~ s/[\n\s]+/,/g;
  253.     $directs =~ s/(^,)+|(,$)//g;
  254.     foreach (split(/,/,$directs)) {
  255.         &error(qq|Invalid relay hostname "$_"|) if &check_hostname($_);
  256.     }
  257.  
  258.     $query->param('directdomains', $directs);
  259.  
  260.     &error("UUCP path-alias database directory must already exist") if 
  261.         ! -d $query->param('pathalias') && $query->param('pathalias') ne '/dev/null';
  262.  
  263.     $error = &check_hostname($query->param('relayname'));
  264.     &error("Relay host name cannot contain shell metacharacters") if $error == 911;
  265.     &error("Invalid relay host name") if $error;
  266. }
  267.  
  268. sub generic {
  269.     &js_title_block($title,$js);
  270.     &header_block($title);
  271.  
  272.     print "<i>$message</i>";
  273.  
  274.     print $query->startform("POST", "", "", "NAME=SendmailForm", "onSubmit=\"return runSubmit(this.form)\"");
  275.  
  276.     print "<center><table cellpadding=5 width=450>\n";
  277.  
  278.     print "<tr><th align=left>Enable sendmail:</th><th align=left>",
  279.         $query->radio_group(-name=>'enable', -values=>['Yes','No'],
  280.                     -default=>$hash{'enable'}),"</th></tr>";
  281.  
  282.     print "<tr><th align=left>Hostname of mail forwarder:</th>\n",
  283.         "<th align=left>",
  284.         $query->textfield(-name=>'forwarder',
  285.                    -default=>$hash{"forwarder"},
  286.                    -size=>20, -maxlength=>256), "</th></tr>";
  287.  
  288.     print "<tr><th align=left>Root domain name:</th>\n",
  289.         "<th align=left>",
  290.         $query->textfield(-name=>'rootdomain',
  291.                    -default=>$hash{"rootdomain"},
  292.                    -size=>20, -maxlength=>256), "</th></tr>";
  293.  
  294.     print "<tr><th align=left>Hostname of mail relay:</th>\n",
  295.         "<th align=left>",
  296.         $query->textfield(-name=>'relayname',
  297.                    -default=>$hash{"relayname"},
  298.                    -size=>20, -maxlength=>256), "</th></tr>";
  299.  
  300.     print "<tr><th align=left>Local domain name:</th>\n",
  301.         "<th align=left>",
  302.         $query->textfield(-name=>'localdomain',
  303.                    -default=>$hash{"localdomain"},
  304.                    -size=>20, -maxlength=>256), "</th></tr>";
  305.  
  306.     print "<tr><th align=left>Directory of the UUCP path-alias database:</th>\n",
  307.         "<th align=left>",
  308.         $query->textfield(-name=>'pathalias',
  309.                    -default=>$hash{"pathalias"},
  310.                    -size=>20, -maxlength=>256), "</th></tr>";
  311.  
  312.     print "<tr><th align=left valign=top>List of domains for which this host
  313.                   is the relay:</th>\n",
  314.         "<th align=left>";
  315.  
  316.     $fix = $query->param('directdomains');
  317.     $fix =~ s/,/\n/g;
  318.     $query->param('directdomains',$fix);
  319.  
  320.     print $query->textarea(-name=>'directdomains', -rows=>4,
  321.                      -default=>$hash{"directdomains"}),
  322.                     "</th></tr>";
  323.  
  324.     print "</table></center><br>";
  325.  
  326.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
  327.  
  328.     print $query->endform;
  329.  
  330. }
  331.